package dialogs;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import controllers.AbstractController;

import system.View;

@SuppressWarnings("serial")
public abstract class AbstractDialog extends JDialog implements ActionListener {
	protected Vector<JTextField> text_fields = new Vector<JTextField>();
	protected View view = null;
	protected AbstractController controller = null;

	// constants used for describing the outcome of an operation
	public static final int OPERATIONSUCCESS = 0;
	public static final int OPERATIONFAILED = 1;
	public static final int VALIDATIONERROR = 2;

	public AbstractDialog(JFrame parent, String title, String table,
			String[] labels, AbstractController c) {
		super(parent, title, true);
		setResizable(false);
		view = (View) parent;
		controller = c;

		JPanel contentPane = new JPanel(new BorderLayout());
		setContentPane(contentPane);
		contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		// this panel will contain the text field labels and the text fields.
		JPanel inputPane = new JPanel();
		inputPane.setBorder(BorderFactory.createCompoundBorder(
				new TitledBorder(new EtchedBorder(), table + " Fields"),
				new EmptyBorder(5, 5, 5, 5)));

		insertLabelsAndFields(inputPane, labels);

		// panel for the OK and cancel buttons
		JPanel buttonPane = new JPanel();
		insertButtons(buttonPane);

		contentPane.add(inputPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});
	}

	//Method for adding buttons to the dialog
	protected abstract void insertButtons(JPanel buttonPane);
	
	//Method for adding fields to the dialog
	protected abstract void insertLabelsAndFields(JPanel inputPane, String[] labels);
	
	protected void close_dialog()
	{
		dispose();
		controller.rollBack();
	}
}
